<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="资产编号" prop="assetNumber">
						<el-input v-model="form.assetNumber" placeholder="请输入资产编号" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="资产名称" prop="assetName">
						<el-input v-model="form.assetName" placeholder="请输入资产名称" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="资产类型" prop="assetType">
						<el-select v-model="form.assetType" placeholder="请选择回款状态">
							<el-option :label="item.label" :value="item.value" v-for="(item, index) in asset_type" :key="index"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="数量" prop="num">
						<el-input v-model="form.num" placeholder="请输入数量" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="单位" prop="unit">
						<el-input v-model="form.unit" placeholder="请输入单位" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="规格" prop="standards">
						<el-input v-model="form.standards" placeholder="请输入规格" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="领取人" prop="userId">
						<el-input v-model="form.userId" placeholder="请输入领取人" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="备注" prop="remark">
						<el-input v-model="form.remark" placeholder="请输入备注" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="AssetRegistrationDialog">
import { useDict } from '@/hooks/dict';
import { useMessage } from '@/hooks/message';
import { getObj, addObj, putObj } from '@/api/oa/assetRegistration';
import { rule } from '@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典
const { asset_type } = useDict('asset_type');

// 提交表单数据
const form = reactive({
	id: '',
	assetNumber: '',
	assetName: '',
	assetType: '',
	num: '',
	unit: '',
	standards: '',
	userId: '',
	remark: '',
});

// 定义校验规则
const dataRules = ref({
	assetNumber: [{ required: true, message: '资产编号不能为空', trigger: 'blur' }],
	assetName: [{ required: true, message: '资产名称不能为空', trigger: 'blur' }],
	assetType: [{ required: true, message: '资产类型不能为空', trigger: 'blur' }],
	num: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
	unit: [{ required: true, message: '单位不能为空', trigger: 'blur' }],
	standards: [{ required: true, message: '规格不能为空', trigger: 'blur' }],
	userId: [{ required: true, message: '领取人不能为空', trigger: 'blur' }],
	remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取assetRegistration信息
	if (id) {
		form.id = id;
		getassetRegistrationData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getassetRegistrationData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
